package ru.dadle8.service.impl.command.training;

import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import ru.dadle8.model.bot.entity.ContentMessage;
import ru.dadle8.model.bot.entity.MenuAction;
import ru.dadle8.model.bot.type.CommandState;
import ru.dadle8.service.impl.command.AbstractCommandService;

@Service
public class TrainingCommandServiceImpl extends AbstractCommandService {

    @Override
    public CommandState getCommandState() {
        return CommandState.TRAINING;
    }

    @Override
    public void processUpdate(Update update) throws TelegramApiException {
        Message inMess = getInMess();
        Long chatId = getChatId();

        String text = null;
        ReplyKeyboard keyboardMarkup = null;

        MenuAction menuAction = getMenuActionService().getMenuAction(inMess.getText());
        if (menuAction != null) {
            text = "Выбери раздел";

            if (menuAction.getType() == 2) {
                ContentMessage contentMessage = getContentMessageService().getStartContentMessage(menuAction.getActionId());
                if (contentMessage != null) {
                    // remove reply keyboard
                    Message message = getBot().execute(getMessageHelper().sendHideKeyboardMessage(chatId, "Готовимся"));
                    getBot().execute(getMessageHelper().sendDeleteMessage(chatId, message.getMessageId()));

                    text = contentMessage.getMessage();
                }
            }

            keyboardMarkup = getKeyboardService().getKeyBoard(menuAction, chatId);
        }

        //Отправка в чат
        if (text == null) {
            text = "Выберите команду.";
        }
        getBot().execute(getMessageHelper().sendMessage(chatId, text, keyboardMarkup));
    }
}
